package gui;

import gui.utils.NetworkInterfaceComboBoxModel;

import java.net.URI;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JButton;
import javax.swing.JTextField;

import common.URIFormatter;


public class PanelNameAndButtons extends javax.swing.JPanel {

	private static final long serialVersionUID = 1L;
	private javax.swing.JButton buttonConnect;
	private javax.swing.JButton buttonReconnect;
	private javax.swing.JButton buttonStop;
	private javax.swing.JComboBox comboBoxNetworkInterface;
	private javax.swing.JLabel labelClientName;
	private javax.swing.JLabel labelNetworkInterface;
	private javax.swing.JTextField textFieldClientName;

	public PanelNameAndButtons() {
		initComponents();
	}

	private void buttonConnectActionPerformed(java.awt.event.ActionEvent evt) {
		textFieldClientName.setEnabled(false);
		comboBoxNetworkInterface.setEnabled(false);
		buttonStop.setEnabled(true);
		buttonReconnect.setEnabled(true);
		buttonConnect.setEnabled(false);
		try {
			ClientGuiLauncher.clientFrame.client.start(comboBoxNetworkInterface
					.getSelectedItem().toString(), new URI(
					ClientGuiLauncher.clientFrame.brokerManagerURI),
					textFieldClientName.getText());
		} catch (Exception e) {
			Logger.getLogger(PanelNameAndButtons.class.getName()).log(
					Level.SEVERE, null, e);
		}
		ClientGuiLauncher.clientFrame.getPanelTopicManagement()
				.setInitialComponentsState();
		ClientGuiLauncher.clientFrame.getPanelPublish()
				.setInitialComponentsState();
		ClientGuiLauncher.clientFrame.getPanelMultiPublish()
				.setInitialComponentsState();

		ClientGuiLauncher.clientFrame
				.getPanelIP()
				.getTextFieldClientIP()
				.setText(
						URIFormatter
								.format(ClientGuiLauncher.clientFrame.client
										.getLocalURI()));
		ClientGuiLauncher.clientFrame
				.getPanelIP()
				.getTextFieldBrokerIP()
				.setText(
						URIFormatter
								.format(ClientGuiLauncher.clientFrame.client
										.getMyBroker().uri));

	}

	private void buttonReconnectActionPerformed(java.awt.event.ActionEvent evt) {
		try {
			ClientGuiLauncher.clientFrame.client.stop(false);
			ClientGuiLauncher.clientFrame.client.start(comboBoxNetworkInterface
					.getSelectedItem().toString(), new URI(
					ClientGuiLauncher.clientFrame.brokerManagerURI),
					textFieldClientName.getText());
		} catch (Exception e) {
			Logger.getLogger(PanelNameAndButtons.class.getName()).log(
					Level.SEVERE, null, e);
		}
		buttonStop.setEnabled(true);
		ClientGuiLauncher.clientFrame.getPanelTopicManagement().disableAll();
		ClientGuiLauncher.clientFrame.getPanelPublish().disableAll();
		ClientGuiLauncher.clientFrame.getPanelMultiPublish().disableAll();
		ClientGuiLauncher.clientFrame.getPanelTopicManagement()
				.setInitialComponentsState();
		ClientGuiLauncher.clientFrame.getPanelPublish()
				.setInitialComponentsState();
		ClientGuiLauncher.clientFrame.getPanelMultiPublish()
				.setInitialComponentsState();
		ClientGuiLauncher.clientFrame
				.getPanelIP()
				.getTextFieldBrokerIP()
				.setText(
						URIFormatter
								.format(ClientGuiLauncher.clientFrame.client
										.getMyBroker().uri));
	}

	private void buttonStopActionPerformed(java.awt.event.ActionEvent evt) {
		try {
			ClientGuiLauncher.clientFrame.client.stop(false);
		} catch (Exception e) {
			Logger.getLogger(PanelNameAndButtons.class.getName()).log(
					Level.SEVERE, null, e);
		}
		buttonStop.setEnabled(false);
		buttonConnect.setEnabled(true);
		buttonReconnect.setEnabled(false);
		ClientGuiLauncher.clientFrame.getPanelIP().getTextFieldBrokerIP()
				.setText("");
		ClientGuiLauncher.clientFrame.getPanelTopicManagement().disableAll();
		ClientGuiLauncher.clientFrame.getPanelPublish().disableAll();
		ClientGuiLauncher.clientFrame.getPanelMultiPublish().disableAll();
	}

	private void comboBoxNetworkInterfaceActionPerformed(
			java.awt.event.ActionEvent evt) {
		if (comboBoxNetworkInterface.getSelectedItem().equals(""))
			buttonConnect.setEnabled(false);
		else if (!textFieldClientName.getText().equals(""))
			buttonConnect.setEnabled(true);
	}

	public JButton getButtonReconnect() {
		return buttonReconnect;
	}

	public JButton getButtonStop() {
		return buttonStop;
	}

	public JTextField getTextFieldClientName() {
		return textFieldClientName;
	}

	private void initComponents() {

		labelClientName = new javax.swing.JLabel();
		textFieldClientName = new javax.swing.JTextField();
		buttonConnect = new javax.swing.JButton();
		buttonReconnect = new javax.swing.JButton();
		buttonStop = new javax.swing.JButton();
		labelNetworkInterface = new javax.swing.JLabel();
		comboBoxNetworkInterface = new javax.swing.JComboBox();

		setPreferredSize(new java.awt.Dimension(365, 46));

		labelClientName.setText("Client's name :");

		textFieldClientName.addKeyListener(new java.awt.event.KeyAdapter() {
			@Override
			public void keyReleased(java.awt.event.KeyEvent evt) {
				textFieldClientNameKeyReleased(evt);
			}
		});

		buttonConnect.setText("Connect");
		buttonConnect.setEnabled(false);
		buttonConnect.setMargin(new java.awt.Insets(2, 5, 2, 5));
		buttonConnect.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				buttonConnectActionPerformed(evt);
			}
		});

		buttonReconnect.setText("Reconnect");
		buttonReconnect.setEnabled(false);
		buttonReconnect.setMargin(new java.awt.Insets(2, 1, 2, 1));
		buttonReconnect.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				buttonReconnectActionPerformed(evt);
			}
		});

		buttonStop.setText("Stop");
		buttonStop.setEnabled(false);
		buttonStop.setMargin(new java.awt.Insets(2, 5, 2, 5));
		buttonStop.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				buttonStopActionPerformed(evt);
			}
		});

		labelNetworkInterface.setText("Network interface :");

		comboBoxNetworkInterface.setModel(new NetworkInterfaceComboBoxModel());
		comboBoxNetworkInterface
				.addActionListener(new java.awt.event.ActionListener() {
					@Override
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						comboBoxNetworkInterfaceActionPerformed(evt);
					}
				});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						javax.swing.GroupLayout.Alignment.TRAILING,
						layout.createSequentialGroup()
								.addComponent(labelNetworkInterface)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(comboBoxNetworkInterface, 0, 73,
										Short.MAX_VALUE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(buttonConnect,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										80,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(buttonStop,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										60,
										javax.swing.GroupLayout.PREFERRED_SIZE))
				.addGroup(
						layout.createSequentialGroup()
								.addComponent(labelClientName)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(textFieldClientName,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										184, Short.MAX_VALUE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(buttonReconnect,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										100,
										javax.swing.GroupLayout.PREFERRED_SIZE)));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(labelClientName)
												.addComponent(
														textFieldClientName,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(
														buttonReconnect,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														20,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(
														buttonStop,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														20,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(
														labelNetworkInterface)
												.addComponent(
														buttonConnect,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														20,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(
														comboBoxNetworkInterface,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														20,
														javax.swing.GroupLayout.PREFERRED_SIZE))));
	}

	private void textFieldClientNameKeyReleased(java.awt.event.KeyEvent evt) {
		if (textFieldClientName.getText().equals(""))
			buttonConnect.setEnabled(false);
		else if (!comboBoxNetworkInterface.getSelectedItem().equals(""))
			buttonConnect.setEnabled(true);
	}
}
